<?php

namespace App\Service\users;

use App\Models\users\UsersAlipayModel;
use App\Models\users\UsersAuthenticationModel;
use App\Models\users\UsersBankModel;
use App\Models\users\UsersModel;
use lxs\config\BaseStatic;

/**
 * 用户保存服务
 */
class UsersSaveService extends BaseStatic
{

    /**
     * @author 实名认证保存
     */
    public function saveRealName($uid, $formData): array
    {
        if (UsersAuthenticationModel::where('uid', $uid)->where('status', 0)->where('deleted_at', 0)->exists()) {
            return self::serviceReturn(400, '您已提交过实名认证，请等待审核结果');
        }
        $Authentication = UsersAuthenticationModel::where('uid', $uid)->first();
        if (!$Authentication) {
            $Authentication = new UsersAuthenticationModel();
        }
        $Authentication->uid = $uid;
        $Authentication->mobile = $formData['mobile'];
        $Authentication->realname = $formData['realname'];
        $Authentication->id_card = $formData['id_card'];
        $Authentication->status = 0;
        $Authentication->deleted_at = 0;
        $Authentication->save();

        if ($Authentication->id) {
            return self::serviceReturn(200, '实名认证保存成功');
        }
        return self::serviceReturn(400, '实名认证保存失败');
    }


    /**
     * 支付宝账户保存
     */
    public function saveAlipayAccount($uid, $data): array
    {
        $alipayAccount = UsersAlipayModel::where('uid', $uid)->first();
        if (!$alipayAccount) {
            $alipayAccount = new UsersAlipayModel();
            $alipayAccount->uid = $uid;
        }
        $alipayAccount->payee_account = $data['payee_account'];
        $alipayAccount->payee_name = $data['payee_name'];
        $alipayAccount->save();

        return self::serviceReturn(200, '绑定支付宝账户成功');
    }


    /**
     * 银行卡信息保存
     */
    public function saveBankCard($uid, $data): array
    {
        $bankBind = UsersBankModel::where('uid', $data)->first();
        if (!$bankBind) {
            $bankBind = new UsersBankModel();
            $bankBind->uid = $uid;
            $bankBind->create_time = time();
        }
        $bankBind->payee = $data['payee'];
        $bankBind->bank_number = $data['bank_number'];
        $bankBind->bank_name = $data['bank_name'];
        $bankBind->bank_info = $data['bank_info'];
        $bankBind->save();

        return self::serviceReturn(200, '绑定银行卡成功');
    }


    /**
     * 注销账户保存
     */
    public function saveCancelAccount($uid): array
    {
        $user = UsersModel::where('id', $uid)->first();
        if (!$user) {
            return self::serviceReturn(400, '用户不存在');
        }
        $user->is_cancel = 1;
        $user->cancel_time = time();
        $user->save();

        return self::serviceReturn(200, '您已成功提交注销申请');
    }


}
